package com.cskaoyan.mall.controller;

import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.service.AdminServiceImpl;
import com.cskaoyan.mall.service.AdminService;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

@WebServlet( "/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    Gson gson = new Gson();
    private AdminService adminService = new AdminServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        //得到login  allAdmins...
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("login".equals(action)){
            login(request,response);
        }else if ("getSearchAdmins".equals(action)){
            try {
                getSearchAdmins(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if ("updateAdminss".equals(action)){
            updateAdminss(request,response);
        }else if ("addAdminss".equals(action)){
            addAdminss(request,response);
        }
    }

    /**
     * 添加管理员账户
     * @param request
     * @param response
     */
    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        adminService.addAdminss(admin);
        Result result = new Result();
        result.setCode(0);
        result.setData(admin);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 修改账户信息逻辑
     * @param request
     * @param response
     */
    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        adminService.updateAdminss(admin);
        Result result = new Result();
        result.setCode(0);
        result.setData(admin);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 多条件查询管理员账号逻辑
     * 1、请求体参数（json字符串）
     * @param request
     * @param response
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin params = gson.fromJson(requestBody, Admin.class);
        List<Admin> admins = adminService.getSearchAdmins(params);
        Result result = new Result();
        result.setCode(0);
        result.setData(admins);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 登录的具体业务逻辑
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody,Admin.class);
        int code = adminService.login(admin);
        Result result = new Result();
        if (code == 200){
            //返回正确的结果
            result.setCode(0);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("token",admin.getEmail());
            map.put("name",admin.getEmail());
            result.setData(map);
        }else{
            result.setCode(1000);
            result.setMessage("用户或密码错误1");
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        //得到login  allAdmins...
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("allAdmins".equals(action)){
            try {
                allAdimins(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("deleteAdmins".equals(action)){
            deleteAdmins(request,response);
        }else if ("getAdminsInfo".equals(action)){
            getAdminInfo(request,response);
        }
    }

    private void getAdminInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
         Admin admin = adminService.getAdminInfo(id);
         Result result = new Result();
         result.setCode(0);
         result.setData(admin);
         response.getWriter().println(gson.toJson(result));
    }

    /**
     * 删除管理员
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        adminService.deleteAdmins(id);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    private void allAdimins(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        List<Admin> adminList = adminService.allAdmins();
        Result result =  new Result();
        result.setCode(0);
        result.setData(adminList);
        response.getWriter().println(gson.toJson(result));
    }
}
